package com.mida.dms.knw.service.impl;

import com.google.common.collect.Maps;
import com.mida.dms.knw.entity.KnwCommonWordsTree;
import com.mida.dms.knw.entity.KnwCompositeInstanceShortcut;
import com.mida.dms.knw.entity.vo.KnwCompositeInstanceDto;
import com.mida.dms.knw.mapper.KnwCommonWordsTreeMapper;
import com.mida.dms.knw.mapper.KnwCompositeInstanceMapper;
import com.mida.dms.knw.service.KnwCommonWordsTreeService;
import com.mida.dms.knw.service.SoidService;
import com.mida.dms.knw.vo.CommonWordsTreeDto;
import com.mida.dms.knw.vo.CommonWordsTreeVo;
import com.mida.dms.knw.vo.KnwCompositeInstanceVo;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.BaseResult;
import com.mida.tool.common.result.ListResult;
import com.mida.tool.common.utils.CharTransFormUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional
public class KnwCommonWordsTreeServiceImpl extends BaseResult implements KnwCommonWordsTreeService {
    @Autowired
    private KnwCommonWordsTreeMapper knwCommonWordsTreeMapper;
    @Autowired
    private SoidService soidService;
    @Autowired
    private KnwCompositeInstanceMapper knwCompositeInstanceMapper;

    @Override
    @Transactional
    public ActionResult addCommonWordsTree(CommonWordsTreeVo commonWordsTreeVo) {
        ActionResult actionResult = new ActionResult();
        if (ObjectUtils.isEmpty(commonWordsTreeVo.getCreator())) {
            buildActionWarn(actionResult, "人员标识不能为空");
        }
        if (ObjectUtils.isEmpty(commonWordsTreeVo.getBusinessUnitSoid())) {
            buildActionWarn(actionResult, "业务单元标识不能为空");
        }

        Long commonWordsTreeSoid = soidService.getSoid();
        //知识type为4
        soidService.insertOid(commonWordsTreeSoid,4);
        //常用语树形结构分类
        KnwCommonWordsTree knwCommonWordsTree = new KnwCommonWordsTree();
        Long surperCommonWordsTreeSoid = commonWordsTreeVo.getSurperCommonWordsTreeSoid();
        Short share = commonWordsTreeVo.getIsShare();
        Date date = new Date();
        String commonWordsTreeName = commonWordsTreeVo.getCommonWordsTreeName();
        Long creator = commonWordsTreeVo.getCreator();
        Long businessUnitSoid = commonWordsTreeVo.getBusinessUnitSoid();
        //上级常用语树形分类标识
        Integer maxSerialNo = null;
        if (ObjectUtils.isNotEmpty(surperCommonWordsTreeSoid) && surperCommonWordsTreeSoid != 0) {
            //子节点
            //查询此节点的同级节点名字是否重名
            List<KnwCommonWordsTree> knwCommonWordsTreeList = knwCommonWordsTreeMapper.selectBySuperTreeSoids(surperCommonWordsTreeSoid,creator,businessUnitSoid);
            if(CollectionUtils.isNotEmpty(knwCommonWordsTreeList)){
                for (KnwCommonWordsTree commonWordsTree : knwCommonWordsTreeList) {
                    String commonWordsTreeName1 = commonWordsTree.getCommonWordsTreeName();
                    if (commonWordsTreeName1.matches(commonWordsTreeName)) {
                        buildActionWarn(actionResult, "同级文件名不能重复");
                        return actionResult;
                    }
                }
            }
            //如果父节点有常用语，那么不能新增此子节点
            int count = knwCompositeInstanceMapper.count(surperCommonWordsTreeSoid);
            if (count > 0) {
                buildActionWarn(actionResult, "此节点有常用语，无法建立子节点");
                return actionResult;
            }
            //查询父节点是否是被分享的，如果是，此子节点也被分享
            KnwCommonWordsTree knwCommonWordsTree1 = knwCommonWordsTreeMapper.selectByPrimaryKey(surperCommonWordsTreeSoid);
            if (ObjectUtils.isNotEmpty(knwCommonWordsTree1)) {
                Short isShare = knwCommonWordsTree1.getIsShare();
                if (isShare == 0) {
                    //父节点没有被分享
                    //设置子节点
                    knwCommonWordsTree.setIsShare(new Short("0"));
                } else if (isShare == 1) {
                    //父节点被分享
                    knwCommonWordsTree.setIsShare(new Short("1"));
                }
            }
            knwCommonWordsTree.setSurperCommonWordsTreeSoid(surperCommonWordsTreeSoid);

        } else {
            //父节点
            knwCommonWordsTree.setSurperCommonWordsTreeSoid(new Long("0"));
            knwCommonWordsTree.setIsShare(new Short("0"));
            //是否重名
            //查询此节点的同级节点名字是否重名
            List<KnwCommonWordsTree> knwCommonWordsTreeList = knwCommonWordsTreeMapper.selectBySuperTreeSoids(surperCommonWordsTreeSoid,creator,businessUnitSoid);
           if(CollectionUtils.isNotEmpty(knwCommonWordsTreeList)){
               for (KnwCommonWordsTree commonWordsTree : knwCommonWordsTreeList) {
                   String commonWordsTreeName1 = commonWordsTree.getCommonWordsTreeName();
                   if (commonWordsTreeName1.matches(commonWordsTreeName)) {
                       buildActionWarn(actionResult, "同级文件名不能重复");
                       return actionResult;
                   }
               }
           }
        }

        //设置序号
        //查出序号最大值
        maxSerialNo = knwCommonWordsTreeMapper.selectMax(knwCommonWordsTree.getSurperCommonWordsTreeSoid(),businessUnitSoid);
        if (ObjectUtils.isEmpty(maxSerialNo)) {
            maxSerialNo = 0;
        }
        maxSerialNo++;
        knwCommonWordsTree.setSerialNo(maxSerialNo.shortValue());
        //常用语树形分类标识
        knwCommonWordsTree.setCommonWordsTreeSoid(commonWordsTreeSoid);
        //常用语树形分类名称
        knwCommonWordsTree.setCommonWordsTreeName(commonWordsTreeName);
        //加五笔码和拼音码
        knwCommonWordsTree.setPyCode(CharTransFormUtil.toPinyinSimple(commonWordsTreeName));
        knwCommonWordsTree.setWbCode(CharTransFormUtil.toWubi(commonWordsTreeName));
        //149L启用
        knwCommonWordsTree.setStatus(149L);

        //创建人
        knwCommonWordsTree.setCreator(new Long(commonWordsTreeVo.getCreator()));
        knwCommonWordsTree.setCreateDate(date);
        knwCommonWordsTree.setUpdateDate(date);
        knwCommonWordsTree.setIsDelete(new Long("0"));
        //组织标识
        knwCommonWordsTree.setOrgInfoSoid(commonWordsTreeVo.getOrgInfoSoid());
        knwCommonWordsTree.setBusinessUnitSoid(commonWordsTreeVo.getBusinessUnitSoid());

        knwCommonWordsTreeMapper.insert(knwCommonWordsTree);
        buildActionSuccess(actionResult);
        return actionResult;
    }



    /**
     * 2、删除常用语
     *
     * @param commonWordsTreeVo
     * @return
     */
    @Override
    @Transactional
    public ActionResult deleteCommonWordsTree(CommonWordsTreeVo commonWordsTreeVo) {
        ActionResult actionResult = new ActionResult();
        Long commonWordsTreeSoid = commonWordsTreeVo.getCommonWordsTreeSoid();
        Long surperCommonWordsTreeSoid = commonWordsTreeVo.getSurperCommonWordsTreeSoid();

        Long creator = commonWordsTreeVo.getCreator();
        if (ObjectUtils.isEmpty(commonWordsTreeSoid)) {
            buildActionWarn(actionResult, "常用语树形分类标识不能为空");
        }
        if (ObjectUtils.isEmpty(creator)) {
            buildActionWarn(actionResult, "人员标识不能为空");
        }
        List<CommonWordsTreeVo> knwCommonWordsTreeList = knwCommonWordsTreeMapper.selectAllTree();
        Map<Long, List<CommonWordsTreeVo>> collect = knwCommonWordsTreeList.stream().collect(Collectors.groupingBy(CommonWordsTreeVo::getSurperCommonWordsTreeSoid));

        List<Long> allChreidSoids = GetChildren(commonWordsTreeSoid, collect);
        //先删除当前节点下所有子节点
        List<KnwCommonWordsTree> knwCommonWordsTrees = new ArrayList<>();
        for (Long treeSoid : allChreidSoids) {
            KnwCommonWordsTree knwCommonWordsTree = new KnwCommonWordsTree();
            knwCommonWordsTree.setIsDelete(1L);
            knwCommonWordsTree.setCommonWordsTreeSoid(treeSoid);
            knwCommonWordsTrees.add(knwCommonWordsTree);
        }
        //todo 删除子节点
        knwCommonWordsTreeMapper.deleteCommonWordsTree(knwCommonWordsTrees);

        //2排序
        //2.1查询父节点下所有子节点
        List<KnwCommonWordsTree> commonWordsTrees = knwCommonWordsTreeMapper.selectBySuperTreeSoid(surperCommonWordsTreeSoid);
        short i = 1;
        if (ObjectUtils.isNotEmpty(commonWordsTrees)) {
            for (KnwCommonWordsTree commonWordsTree : commonWordsTrees) {
                commonWordsTree.setSerialNo(i++);
            }
        }
        //批量更新序号
        knwCommonWordsTreeMapper.updateSeriaNoForList(commonWordsTrees);

//        Short serialNo = null;
//
//        BeanUtils.copyProperties(commonWordsTreeVo, knwCommonWordsTree);
//        //判断删除的是父节点还是子节点
//        if (ObjectUtils.isEmpty(surperCommonWordsTreeSoid)
//                && ObjectUtils.isNotEmpty(commonWordsTreeSoid)) {//删除父节点
//
//            //1先删除
//            //1.1删除子节点
//            knwCommonWordsTreeMapper.deleteBySurperTreeSoid(commonWordsTreeSoid);
//            //1.2删除根节点
//            knwCommonWordsTreeMapper.deleteByPrimaryKey(commonWordsTreeSoid);
//
//            //2排序
//            List<KnwCommonWordsTree> commonWordsTrees = knwCommonWordsTreeMapper.selectSuperTree();
//            short i = 1;
//            if (ObjectUtils.isNotEmpty(commonWordsTrees)) {
//                for (KnwCommonWordsTree commonWordsTree : commonWordsTrees) {
//                    commonWordsTree.setSerialNo(i++);
//                }
//            }
//            //批量更新序号
//            knwCommonWordsTreeMapper.updateSeriaNoForList(commonWordsTrees);
//        } else if (ObjectUtils.isNotEmpty(surperCommonWordsTreeSoid)
//                && ObjectUtils.isNotEmpty(commonWordsTreeSoid)) {//删除子节点
//            //1.先删除
//            knwCommonWordsTreeMapper.deleteByPrimaryKey(commonWordsTreeSoid);

//        }
        buildActionSuccess(actionResult);
        return actionResult;
    }

    @Override
    @Transactional
    public ActionResult updateCommonWordsTree(CommonWordsTreeVo commonWordsTreeVo) {
        ActionResult actionResult = new ActionResult();
        Long commonWordsTreeSoid = commonWordsTreeVo.getCommonWordsTreeSoid();
        Long creator = commonWordsTreeVo.getCreator();
        if (ObjectUtils.isEmpty(commonWordsTreeSoid)) {
            buildActionWarn(actionResult, "常用语树形分类标识不能为空");
        }
        if (ObjectUtils.isEmpty(commonWordsTreeVo.getCommonWordsTreeName())) {
            buildActionWarn(actionResult, "常用语树形分类名称不能为空");
        }
        if (ObjectUtils.isEmpty(creator)) {
            buildActionWarn(actionResult, "人员标识不能为空");
        }
        KnwCommonWordsTree knwCommonWordsTree = new KnwCommonWordsTree();
        knwCommonWordsTree.setCommonWordsTreeSoid(new Long(commonWordsTreeSoid));
        knwCommonWordsTree.setCommonWordsTreeName(commonWordsTreeVo.getCommonWordsTreeName());
        knwCommonWordsTree.setBusinessUnitSoid(commonWordsTreeVo.getBusinessUnitSoid());
        knwCommonWordsTree.setOrgInfoSoid(commonWordsTreeVo.getOrgInfoSoid());
        knwCommonWordsTree.setUpdateUser(new Long(commonWordsTreeVo.getCreator()));
        knwCommonWordsTree.setUpdateDate(new Date());
        knwCommonWordsTreeMapper.updateByPrimaryKeySelective(knwCommonWordsTree);
        buildActionSuccess(actionResult);
        return actionResult;
    }

    @Override
    @Transactional
    public ActionResult sortCommonWordsTree(List<KnwCommonWordsTree> commonWordsTreeSoids) {
        ActionResult actionResult = new ActionResult();
        if (CollectionUtils.isEmpty(commonWordsTreeSoids)) {
            buildActionWarn(actionResult, "排序节点集合不能为空");
        }
        //批量更新序号
        knwCommonWordsTreeMapper.updateSeriaNoForList(commonWordsTreeSoids);
        buildActionSuccess(actionResult);
        return actionResult;
    }

    @Override
    public ListResult<List<CommonWordsTreeVo>> getCommonWordsTree(CommonWordsTreeVo commonWordsTreeVo) {
        ListResult<List<CommonWordsTreeVo>> listListResult = new ListResult<>();
        Map<String, Object> mapParm = new HashMap<String, Object>();
        if (commonWordsTreeVo.getBusinessUnitSoid() != null) {
            mapParm.put("businessUnitSoid", commonWordsTreeVo.getBusinessUnitSoid());
        }

        if (commonWordsTreeVo.getCreator() != null) {
            mapParm.put("creator", commonWordsTreeVo.getCreator());
        }

        // 根据业务单元、用户标识查询 相关自定义标签列表
        List<CommonWordsTreeVo> knwCommonWordsTrees = knwCommonWordsTreeMapper.getKnwCommonWordsTree(mapParm);

        //将所有数据放入longCommonWordsTreeVoHashMap
        Map<Long, CommonWordsTreeVo> longCommonWordsTreeVoHashMap = knwCommonWordsTrees.stream().collect(Collectors.toMap(CommonWordsTreeVo::getCommonWordsTreeSoid, e -> e));
        if (knwCommonWordsTrees != null && knwCommonWordsTrees.size() > 0) {

            //将根节点的数据放入临时List
            List<CommonWordsTreeVo> list = knwCommonWordsTrees.stream().filter(vo -> vo.getSurperCommonWordsTreeSoid().equals(0L)).collect(Collectors.toList());

            //在longCommonWordsTreeVoHashMap中做遍历
            for (Map.Entry<Long, CommonWordsTreeVo> longCommonWordsTreeVoEntry : longCommonWordsTreeVoHashMap.entrySet()) {
                Long commonWordsTreeSoid = longCommonWordsTreeVoEntry.getKey();
                CommonWordsTreeVo value = longCommonWordsTreeVoEntry.getValue();
                Long surperCommonWordsTreeSoid = value.getSurperCommonWordsTreeSoid();

                CommonWordsTreeVo commonWordsTreeVo1 = longCommonWordsTreeVoHashMap.get(surperCommonWordsTreeSoid);
                if (ObjectUtils.isEmpty(commonWordsTreeVo1)) {
                    continue;
                }
                //每新增一层，都要new ArrayList<>()
                if (ObjectUtils.isEmpty(commonWordsTreeVo1) || ObjectUtils.isEmpty(commonWordsTreeVo1.getCommonWordsTreeVos())) {
                    List<CommonWordsTreeVo> commonWordsTreeVoList = new ArrayList<>();
                    commonWordsTreeVoList.add(value);
                    commonWordsTreeVo1.setCommonWordsTreeVos(commonWordsTreeVoList);
                } else {
                    //同层之间，在一个list中新增
                    commonWordsTreeVo1.getCommonWordsTreeVos().add(value);
                }
            }
            buildListSuccess(listListResult, list);
        }
        return listListResult;
    }

    /**
     * 根据map的key排序
     *
     * @param map    待排序的map
     * @param isDesc 是否降序，true：降序，false：升序
     * @return 排序好的map
     */
    private static <K extends Comparable<? super K>, V> Map<K, V> sortByKey(Map<K, V> map, boolean isDesc) throws Exception {
        Map<K, V> result = Maps.newLinkedHashMap();
        if (isDesc) {
            map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey().reversed())
                    .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        } else {
            map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey())
                    .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        }
        return result;
    }


    @Override
    public ListResult<List<CommonWordsTreeVo>> selectCommonWordsTrees(CommonWordsTreeVo commonWordsTreeVo) {
        ListResult<List<CommonWordsTreeVo>> listListResult = new ListResult<>();
        Long creator = commonWordsTreeVo.getCreator();
        String queryName = commonWordsTreeVo.getQueryName();
        Map<String, Object> stringLongHashMap = new HashMap<>();
        stringLongHashMap.put("creator", creator);
        //查询个人的树形所有数据
        List<CommonWordsTreeVo> knwCommonWordsTreeList = knwCommonWordsTreeMapper.selectPersonalTreeList(stringLongHashMap);
        //分组
        Map<Long, List<CommonWordsTreeVo>> collect = knwCommonWordsTreeList.stream().collect(Collectors.groupingBy(CommonWordsTreeVo::getSurperCommonWordsTreeSoid));
        //将所有数据放入longCommonWordsTreeVoHashMap
        Map<Long, CommonWordsTreeVo> longCommonWordsTreeVoHashMap = knwCommonWordsTreeList.stream().collect(Collectors.toMap(CommonWordsTreeVo::getCommonWordsTreeSoid, e -> e));
        if (StringUtils.isEmpty(queryName)) {
            //将根节点的数据放入临时map（返回此map）
            List<CommonWordsTreeVo> collectList = knwCommonWordsTreeList.stream().filter(vo -> vo.getSurperCommonWordsTreeSoid().equals(0L)).collect(Collectors.toList());
            //在longCommonWordsTreeVoHashMap中做遍历
            selectTree(longCommonWordsTreeVoHashMap);

            buildListSuccess(listListResult, collectList);
            return listListResult;
        } else {
            //搜索
            //找到搜索的节点
            List<KnwCompositeInstanceDto> list = knwCompositeInstanceMapper.search(queryName, creator);
            //根据CommonWordsTreeSoid去重
            TreeSet<KnwCompositeInstanceDto> collect2 = list.stream().collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(e -> e.getCommonWordsTreeSoid()))));
            List<KnwCompositeInstanceDto> collect3 = collect2.stream().collect(Collectors.toList());
            List<Long> commonWordsTreeVoList = new ArrayList<>();
            List<CommonWordsTreeVo> commonWordsTreeVos1 = new ArrayList<>();
            for (KnwCompositeInstanceDto wordsTreeVo : collect3) {
                //此节点的soid和vo
                Long commonWordsTreeSoid = wordsTreeVo.getCommonWordsTreeSoid();
                CommonWordsTreeVo commonWordsTreeVo1 = longCommonWordsTreeVoHashMap.get(commonWordsTreeSoid);
                if(ObjectUtils.isEmpty(commonWordsTreeVo1)){
                    continue;
                }
                //父节点的soid和vo
                Long surperCommonWordsTreeSoid = commonWordsTreeVo1.getSurperCommonWordsTreeSoid();
                CommonWordsTreeVo commonWordsTreeVo3 = longCommonWordsTreeVoHashMap.get(surperCommonWordsTreeSoid);

                if (commonWordsTreeVoList.contains(surperCommonWordsTreeSoid)) {
                    //同级的节点放在一个list中
                    commonWordsTreeVo3.getCommonWordsTreeVos().add(commonWordsTreeVo1);
                } else {
                    commonWordsTreeVoList.add(surperCommonWordsTreeSoid);
                    //不是同级的找上级
                    //递归查出此节点+此节点的所有父节点
                    List<Long> longs = new ArrayList<>();
                    longs = recursiveMethod(commonWordsTreeSoid, longCommonWordsTreeVoHashMap, longs);
                    //根节点
                    Long rootcommonWordsTreeSoid = longs.get(longs.size() - 1);
                    CommonWordsTreeVo commonWordsTreeVo2 = longCommonWordsTreeVoHashMap.get(rootcommonWordsTreeSoid);
                    //将根节点的vo放入commonWordsTreeVos1中最终返回
                    commonWordsTreeVos1.add(commonWordsTreeVo2);
                    Map<Long, CommonWordsTreeVo> collect1 = longs.stream().collect(Collectors.toMap(e -> e, vo -> longCommonWordsTreeVoHashMap.get(vo)));
                    //将collect1变成树形
                    selectTree(collect1);

                }
            }
            //去重两个一样的树结构
            TreeSet<CommonWordsTreeVo> collects = commonWordsTreeVos1.stream().collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(e -> e.getCommonWordsTreeSoid()))));
            List<CommonWordsTreeVo> commonWordsTreeVos2 = collects.stream().collect(Collectors.toList());
            buildListSuccess(listListResult, commonWordsTreeVos2);
            return listListResult;
        }
    }

    private void selectTree(Map<Long, CommonWordsTreeVo> longCommonWordsTreeVoHashMap) {
        for (Map.Entry<Long, CommonWordsTreeVo> longCommonWordsTreeVoEntry : longCommonWordsTreeVoHashMap.entrySet()) {
            Long commonWordsTreeSoid = longCommonWordsTreeVoEntry.getKey();
            CommonWordsTreeVo value = longCommonWordsTreeVoEntry.getValue();
            Long surperCommonWordsTreeSoid = value.getSurperCommonWordsTreeSoid();

            CommonWordsTreeVo commonWordsTreeVo1 = longCommonWordsTreeVoHashMap.get(surperCommonWordsTreeSoid);
            if (ObjectUtils.isEmpty(commonWordsTreeVo1)) {
                continue;
            }
            //每新增一层，都要new ArrayList<>()
            if (ObjectUtils.isEmpty(commonWordsTreeVo1) || ObjectUtils.isEmpty(commonWordsTreeVo1.getCommonWordsTreeVos())) {
                List<CommonWordsTreeVo> commonWordsTreeVoList = new ArrayList<>();
                commonWordsTreeVoList.add(value);
                commonWordsTreeVo1.setCommonWordsTreeVos(commonWordsTreeVoList);
            } else {
                //同层之间，在一个list中新增
                commonWordsTreeVo1.getCommonWordsTreeVos().add(value);
            }
        }
    }


    @Override
    @Transactional
    public ActionResult shareCommonWordsTrees(CommonWordsTreeVo commonWordsTreeVo) {
        ActionResult actionResult = new ActionResult();
        Map<String, Object> stringLongHashMap = new HashMap<>();
        Short isShare = commonWordsTreeVo.getIsShare();
        Long commonWordsTreeSoid = commonWordsTreeVo.getCommonWordsTreeSoid();
        Long businessUnitSoid = commonWordsTreeVo.getBusinessUnitSoid();
        Long creator = commonWordsTreeVo.getCreator();
        stringLongHashMap.put("businessUnitSoid", businessUnitSoid);
        stringLongHashMap.put("creator", creator);
        List<CommonWordsTreeVo> knwCommonWordsTreeList = knwCommonWordsTreeMapper.selectTreeList(stringLongHashMap);
        //全部数据的map
        Map<Long, CommonWordsTreeVo> collectMap = knwCommonWordsTreeList.stream().collect(Collectors.toMap(CommonWordsTreeVo::getCommonWordsTreeSoid, e -> e));
        //分组
        Map<Long, List<CommonWordsTreeVo>> collect = knwCommonWordsTreeList.stream().collect(Collectors.groupingBy(CommonWordsTreeVo::getSurperCommonWordsTreeSoid));
        List<Long> longs = new ArrayList<>();
        //递归查出此节点的所有父节点
        longs = recursiveMethod(commonWordsTreeSoid, collectMap, longs);
        //递归查出此节点的所有子节点
        List<Long> allChreidSoids = GetChildren(commonWordsTreeSoid, collect);

        if (isShare == 0) {
            cansoleShare(isShare, commonWordsTreeSoid, longs, allChreidSoids);
        } else {
            //分享
            //修改父节点和此节点都为分享
            knwCommonWordsTreeMapper.updateChildTreeSoid(longs, isShare);
            //修改此节点的子节点为分享
            knwCommonWordsTreeMapper.updateChildTreeSoid(allChreidSoids, isShare);
        }

        buildActionSuccess(actionResult);
        return actionResult;
    }


    public void cansoleShare(Short isShare, Long commonWordsTreeSoid, List<Long> longs, List<Long> allChreidSoids) {
        //TODO 取消分享当前节点
        //取消分享/分享此节点
        KnwCommonWordsTree knwCommonWordsTree1 = new KnwCommonWordsTree();
        knwCommonWordsTree1.setIsShare(isShare);
        knwCommonWordsTree1.setCommonWordsTreeSoid(commonWordsTreeSoid);
        knwCommonWordsTreeMapper.updateByPrimaryKeySelective(knwCommonWordsTree1);
        //取消当前节点子节点的集合
        if (CollectionUtils.isNotEmpty(allChreidSoids)) {
            knwCommonWordsTreeMapper.updateChildTreeSoid(allChreidSoids, isShare);
        }
        //TODO 查出当前节点的父节点集合
        //从最低一级遍历各级节点
        for (int i = 1; i < longs.size(); i++) {
            Long surperCommonWordsTreeSoid = longs.get(i);
            Integer count = knwCommonWordsTreeMapper.countIsShare(surperCommonWordsTreeSoid);
            if (count > 0) {
                break;
            } else {
                //TODO 取消分享
                KnwCommonWordsTree knwCommonWordsTree = new KnwCommonWordsTree();
                knwCommonWordsTree.setIsShare(isShare);
                knwCommonWordsTree.setCommonWordsTreeSoid(surperCommonWordsTreeSoid);
                knwCommonWordsTreeMapper.updateByPrimaryKeySelective(knwCommonWordsTree);
            }
        }

    }

    private List<Long> recursiveMethod(Long commonWordsTreeSoid, Map<Long, CommonWordsTreeVo> collectMap, List<Long> longs) {
        //取出此节点的vo
        CommonWordsTreeVo commonWordsTreeVo2 = collectMap.get(commonWordsTreeSoid);
        //取出此节点的父节点
        Long surperCommonWordsTreeSoid = commonWordsTreeVo2.getSurperCommonWordsTreeSoid();
        longs.add(commonWordsTreeSoid);
        if (!surperCommonWordsTreeSoid.equals(0L)) {
            recursiveMethod(surperCommonWordsTreeSoid, collectMap, longs);
        }
        return longs;
    }

    /**
     * @param commonWordsTreeSoid 节点的soid
     * @param collect             所有父节点对应子节点的分组map
     * @return 当前节点下的所有子节点（包括当前节点）
     */
    private List<Long> GetChildren(Long commonWordsTreeSoid, Map<Long, List<CommonWordsTreeVo>> collect) {
        //取出此节点的vo
        List<CommonWordsTreeVo> all = new ArrayList<>();
        //取出当前节点下所有子节点对象
        List<CommonWordsTreeVo> allChreid = getAllChild(commonWordsTreeSoid, collect, all);
        //取出当前节点下所有子节点的soid
        List<Long> allChreidSoids = allChreid.stream().map(CommonWordsTreeVo::getCommonWordsTreeSoid).collect(Collectors.toList());
        //将当前节点添加到需要删除的节点集合中
        allChreidSoids.add(commonWordsTreeSoid);
        return allChreidSoids;

    }

    /**
     * @param commonWordsTreeSoid 当前节点的soid
     * @param collect             所有父节点对应子节点的分组map
     * @param all                 子节点对象集合
     * @return 子节点对象集合
     */
    private List<CommonWordsTreeVo> getAllChild(Long commonWordsTreeSoid, Map<Long, List<CommonWordsTreeVo>> collect, List<CommonWordsTreeVo> all) {

        //查询此节点下的子节点
        List<CommonWordsTreeVo> chrid = collect.get(commonWordsTreeSoid);
        if (ObjectUtils.isNotEmpty(chrid)) {
            all.addAll(chrid);
            for (CommonWordsTreeVo commonWordsTreeVo : chrid) {
                //获取当前节点下的子节点
                getAllChild(commonWordsTreeVo.getCommonWordsTreeSoid(), collect, all);
            }
        }
        return all;
    }


    @Override
    public Integer isDelete(CommonWordsTreeVo commonWordsTreeVo) {
        ActionResult actionResult = new ActionResult();
        Long commonWordsTreeSoid = commonWordsTreeVo.getCommonWordsTreeSoid();
        Long creator = commonWordsTreeVo.getCreator();
        if (ObjectUtils.isEmpty(creator)) {
            buildActionWarn(actionResult, "人员标识不能为空");
        }
        if (ObjectUtils.isEmpty(commonWordsTreeSoid)) {
            buildActionWarn(actionResult, "常用语树形分类标识不能为空");
        }
        List<CommonWordsTreeVo> knwCommonWordsTreeList = knwCommonWordsTreeMapper.selectAllTree();
        Map<Long, List<CommonWordsTreeVo>> collect = knwCommonWordsTreeList.stream().collect(Collectors.groupingBy(CommonWordsTreeVo::getSurperCommonWordsTreeSoid));
        List<Long> allChreidSoids = GetChildren(commonWordsTreeSoid, collect);
        //查询节点下是否有常用语
        return knwCommonWordsTreeMapper.queryInstance(allChreidSoids);
    }

}
